package com.yng.simbot01.service;

import com.yng.simbot01.Annotation.RobotCommand;
import love.forte.simbot.event.MessageEvent;
import love.forte.simbot.message.MessageContent;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component
public class RobotCommandService {

    public CompletableFuture<?> doService(MessageEvent event) throws InvocationTargetException, IllegalAccessException {
        MessageContent messageContent = event.getMessageContent();
        String plainText = messageContent.getPlainText();
        if (plainText == null) return null;//非文字内容

        //看该走哪个方法
        Method[] methods = RobotCommandService.class.getDeclaredMethods();
        List<Method> methodsWithAnnotation
                = Arrays.stream(methods)
                .filter(method -> method.isAnnotationPresent(RobotCommand.class))
                .toList();

        for (Method method : methodsWithAnnotation) {
            RobotCommand annotation = method.getAnnotation(RobotCommand.class);
            List<String> list = Arrays.stream(annotation.value()).filter(value -> value.equals(plainText.trim())).toList();
            if(!CollectionUtils.isEmpty(list)){
                //执行方法
                return  (CompletableFuture<?>) method.invoke(null, event);
            }
        }

        return null;
    }

    @RobotCommand({"/早安", "/早","/早上好"})
    private static CompletableFuture<?> goodMorning(MessageEvent event){
        return event.replyAsync("早上好啊~~~");
    }

    @RobotCommand({"/晚上好"})
    private static CompletableFuture<?> goodNight(MessageEvent event){
        return event.replyAsync("晚上好啊~~~");
    }

    @RobotCommand({"/五子棋PK"})
    private static CompletableFuture<?> wuZiQiPK(MessageEvent event){
        return event.replyAsync("消息发起者id: " + event.getAuthorId().toString());
    }

}
